package com.ebai.product.management.controller.system;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ebai.product.management.base.cache.service.PublicCacheImpl;
import com.ebai.product.management.base.controller.WebController;
import com.ebai.product.management.base.utils.Common;
import com.ebai.product.management.model.entity.Role;
import com.ebai.product.management.service.system.RoleService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/system/role")
public class RoleController extends WebController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    @RequiresPermissions("system:role:view")
    public String indexPage(ModelMap modelMap) {
        modelMap.addAttribute("roleTypeList", PublicCacheImpl.getDictListByCode("XTYHLX"));
        return "system/role/index";
    }

    /**
     * AJAX调用分页数据信息
     * @param response HTTP响应
     * @param page 当前页码
     * @param limit 分页条数
     * @param sort 排序方式
     * @param order 排序字段
     * @param condition 查询条件
     */
    @RequestMapping("/pageList")
    public void jsonPageList(HttpSession session, HttpServletResponse response, int page, int limit, String sort, String order, Role condition) {
        JSONObject json = Common.getJsonDatagrid(this.roleService.queryPage(page, limit, condition));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }

    /**
     * 新增或修改时的表单页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/form")
    public String formPage(ModelMap modelMap) {
        return "system/role/form";
    }

    /**
     * 检查角色名称是否重否
     * @param response
     * @param roleName
     */
    @RequestMapping("/checkExist")
    public void jsonDataExist(HttpServletResponse response, String roleName) {
        JSONObject result = Common.getJsonResult(this.roleService.checkExist(roleName));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 表单数据保存
     * @param response
     * @param data
     */
    @RequestMapping("/save")
    public void jsonDataSave(HttpServletResponse response, Role data) {
        JSONObject result = Common.getJsonResult(this.roleService.saveData(data));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 列表数据删除
     * @param response
     * @param ids
     */
    @RequestMapping("/drop")
    public void jsonDataDrop(HttpServletResponse response, String ids) {
        JSONObject result = Common.getJsonResult(this.roleService.dropData(ids));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }
    
    /**
     * 角色权限分配窗口
     * @return
     */
    @RequestMapping("/perms")
    public ModelAndView roleAuthPerms(ModelMap modelMap, String rolePid) {
    	ModelAndView view = new ModelAndView();
    	view.setViewName("system/role/perms");
    	view.addObject("treeAuths", this.roleService.selectAuthTreeData());
    	view.addObject("roleAuthPids", this.roleService.getRoleAuthPids(rolePid));
    	return view;
    }
    
    /**
     * 保存角色权限数据
     * @param response
     * @param data
     */
    @RequestMapping("/saveperms")
    @Transactional(rollbackFor=Exception.class)
    public void jsonDataSavePerms(HttpServletResponse response, String roleid, String authids) {
        JSONObject result = Common.getJsonResult(this.roleService.saveRolePermsData(roleid, authids));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }
}
